﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using MM.Model;
using MM.Common;

namespace MM.DBUtility
{
    public class BLSqlDataChargeV3 : BLSqlDataCharge
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        static BLSqlDataChargeV3()
        {
            ConnStrV3 = System.Configuration.ConfigurationSettings.AppSettings["SQLconnStringV3"].ToString();
        }
        public static string ConnStrV3;

        /// <summary>
        /// 获取该次请求的规则信息
        /// </summary>
        /// <param name="Imsi"></param>
        /// <param name="AreaNO"></param>
        /// <param name="ONO"></param>
        /// <param name="CNO"></param>
        /// <param name="Vn"></param>
        /// <returns></returns>
        public static List<RulesSPinfoV3> GetRules(string Imsi, string AreaNO, string ONO, string CNO, string Phoneid, string Imei, string Device)
        {
            List<RulesSPinfoV3> result = null;
            using (SqlConnection conn = new SqlConnection(ConnStrV3))
            {
                using (SqlDataReader dr = DbHelperSql.ExecuteReaderSP(conn, "SP_Charge",
                   new SqlParameter("@Imsi", Imsi),
                   new SqlParameter("@AreaNO", AreaNO),
                   new SqlParameter("@ONO", ONO),
                   new SqlParameter("@CNO", CNO),
                   new SqlParameter("@Phoneid", Phoneid),
                   new SqlParameter("@Imei", Imei),
                   new SqlParameter("@Device", Device)))
                {
                    if (dr != null && dr.HasRows)
                    {
                        result = DTOHelper.CreateModelList<RulesSPinfoV3>(dr);
                        if (result != null && result.Count > 0)
                        {
                            dr.NextResult();
                            List<Mask> LMaskInfo = DTOHelper.CreateModelList<Mask>(dr);
                            result[0].LMask = LMaskInfo;
                            dr.NextResult();
                            List<Doubles> LDoublesInfo = DTOHelper.CreateModelList<Doubles>(dr);
                            result[0].LDoubles = LDoublesInfo;
                        }
                    }
                    else
                    {
                        result = new List<RulesSPinfoV3>();
                    }
                    dr.Close();
                }
            }
            return result;
        }
        /// <summary>
        /// 3.0版本，计费信息
        /// </summary>
        /// <param name="strXML"></param>
        /// <returns></returns>
        public static string GetChargeInfoV3(string strXML)
        {
            XmlHelper Xhelp = new XmlHelper(strXML);
            //手机ID
            string strPhoneid = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("phone"), "id");
            //
            string strImei = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("phone"), "imei");
            //SIM号码
            string strImsi = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("phone"), "imsi");
            //区号
            string strArea = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("phone"), "area");
            //运营商编号
            string strMno = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("phone"), "mno");
            //客户编号
            string strOrigin = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("gtsbox"), "origin");
            //
            string strDevice = Xhelp.ReadXMLAttributValue(Xhelp.xmlE.Element("info").Element("gtsbox"), "device");

            Xhelp.Dispose();

            if (!string.IsNullOrEmpty(strArea) && !string.IsNullOrEmpty(strMno) && !string.IsNullOrEmpty(strOrigin))
            {

                List<RulesSPinfoV3> LRSPinfo = GetRules(strImsi, strArea, strMno, strOrigin, strPhoneid, strImei, strDevice);
                if (LRSPinfo != null && LRSPinfo.Count > 0)
                {
                    StringBuilder sbResult = new StringBuilder(500);
                    sbResult.Append("<down>");
                    //短信内容（端口,指令）
                    //发送时间间隔
                    //短信条数
                    int intCount = Convert.ToInt32(string.IsNullOrEmpty(LRSPinfo[0].SendCount) ? "0" : LRSPinfo[0].SendCount);
                    for (int i = 0; i < intCount; i++)
                    {
                        sbResult.Append(string.Format("<sendsms><send id=\"\" number=\"{0}\" data=\"{1}\" timer=\"{2}\" /></sendsms>", LRSPinfo[0].ChannelNumber, LRSPinfo[0].ChannelOrder, LRSPinfo[0].SendInterval));
                    }
                    //屏蔽关键字
                    sbResult.Append("<block>");
                    if (LRSPinfo[0].LMask != null && LRSPinfo[0].LMask.Count > 0)
                    {
                        //屏蔽号码数量
                        List<Mask> LMaskInfo = LRSPinfo[0].LMask.Where(m => !string.IsNullOrEmpty(m.TelNumber)).ToList();
                        foreach (Mask mMask in LMaskInfo)
                        {
                            sbResult.Append(string.Format("<number data=\"{0}\" />", mMask.TelNumber));
                        }
                        //屏蔽英文关键字数量
                        LMaskInfo = LRSPinfo[0].LMask.Where(m => !string.IsNullOrEmpty(m.English)).ToList();
                        foreach (Mask mMask in LMaskInfo)
                        {
                            sbResult.Append(string.Format("<english data=\"{0}\" />", mMask.English));
                        }
                        //屏蔽中文关键字数量
                        LMaskInfo = LRSPinfo[0].LMask.Where(m => !string.IsNullOrEmpty(m.Chinese)).ToList();
                        foreach (Mask mMask in LMaskInfo)
                        {
                            sbResult.Append(string.Format("<chinese data=\"{0}\" />", mMask.Chinese));
                        }
                    }
                    sbResult.Append("</block>");

                    //二次确认关键字
                    sbResult.Append("<double>");
                    if (LRSPinfo[0].LDoubles != null && LRSPinfo[0].LDoubles.Count > 0)
                    {
                        //二次确认号码关键字
                        List<Doubles> LDoublesInfo = LRSPinfo[0].LDoubles.Where(m => !string.IsNullOrEmpty(m.TelNumber)).ToList();
                        foreach (Doubles mDoubles in LDoublesInfo)
                        {
                            sbResult.Append(string.Format("<number data=\"{0}\" />", mDoubles.TelNumber));
                        }
                        //屏蔽英文关键字数量
                        LDoublesInfo = LRSPinfo[0].LDoubles.Where(m => !string.IsNullOrEmpty(m.English)).ToList();
                        foreach (Doubles mDoubles in LDoublesInfo)
                        {
                            sbResult.Append(string.Format("<english data=\"{0}\" />", mDoubles.English));
                        }
                        //屏蔽中文关键字数量
                        LDoublesInfo = LRSPinfo[0].LDoubles.Where(m => !string.IsNullOrEmpty(m.Chinese)).ToList();
                        foreach (Doubles mDoubles in LDoublesInfo)
                        {
                            sbResult.Append(string.Format("<chinese data=\"{0}\" />", mDoubles.Chinese));
                        }
                    }
                    sbResult.Append("</double>");
                    sbResult.Append("</down>");

                    //记录成功返回后SIM记录
                    try
                    {
                        SetSIMinfo(ConnStrV3, strArea, strMno, strOrigin, strImsi, LRSPinfo[0].ChannelNO, intCount);
                    }
                    catch (Exception ex) { LogHelp.WriteLogInfo(ex.ToString()); }

                    return sbResult.ToString();
                }
            }

            return "";
        }
    }
}
